<?php

App::uses('Crud', 'Model');
App::uses('Workstation', 'Model');
App::uses('UserArea', 'Model');

class LogCertification extends Crud
{
    const TYPE_CREATE = 1;
    const TYPE_UPDATE = 2;
    const TYPE_DESTROY = 3;

    public function addCreateLog($userId, $record) {
        $record['log_user_id'] = $userId;
        $record['log_type'] = 1;
        $record['record_id'] = $record['id'];
        unset($record['id']);
        return $this->save($record);
    }

    public function addUpdateLog($userId, $record, $comment = "") {
        $record['log_user_id'] = $userId;
        $record['log_type'] = 2;
        $record['log_comment'] = $comment;
        $record['record_id'] = $record['id'];
        unset($record['id']);
        return $this->save($record);
    }

    public function addDestroyLog($userId, $comment, $record) {
        $record['log_user_id'] = $userId;
        $record['log_type'] = 3;
        $record['log_comment'] = $comment;
        $record['record_id'] = $record['id'];
        unset($record['id']);
        return $this->save($record);
    }

    public function getCreateCertificationsByUser($userId) {
        $filters = array(
            'fields' => array(
                'u.full_name',
                'u2.full_name as asociado',
                'm.name as moe',
                'a.name as area',
                'l.name as line',
                'w.description as workstation',
                'LogCertification.log_creation_date',
            ),
            'joins' => array(
                array(
                    'table' => 'certifications',
                    'alias' => 'c',
                    'type' => 'INNER',
                    'conditions' => array(
                        'c.id = LogCertification.id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = LogCertification.log_user_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u2',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u2.id = LogCertification.user_id',
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w.id = LogCertification.workstation_id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.id = w.line_id',
                    ),
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'INNER',
                    'conditions' => array(
                        'a.id = l.area_id',
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'INNER',
                    'conditions' => array(
                        'm.id = a.moe_id',
                    ),
                ),
                array(
                    'table' => 'user_areas',
                    'alias' => 'ua',
                    'type' => 'INNER',
                    'conditions' => array(
                        'ua.status' => UserArea::STATUS_ENABLED,
                        'ua.area_id = a.id',
                        'ua.user_id' => $userId,
                    ),
                ),
            ),
            'conditions' => array(
                'LogCertification.log_type' => 1
            ),
            'order' => array(
                'LogCertification.log_creation_date DESC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getDestroyCertificationsByUser($userId, $startDate = null, $endDate = null)
    {
        if ($startDate === null) {
            $startDate = new DateTime();
            $startDate->modify('-1 month');
        }
        if ($endDate === null) {
            $endDate = new DateTime();
        }
        $filters = array(
            'fields' => array(
                'u.full_name',
                'u2.full_name as asociado',
                'm.name as moe',
                'a.name as area',
                'l.name as line',
                'w.description as workstation',
                'LogCertification.log_comment',
                'LogCertification.log_creation_date',
            ),
            'joins' => array(
                array(
                    'table' => 'certifications',
                    'alias' => 'c',
                    'type' => 'INNER',
                    'conditions' => array(
                        'c.id = LogCertification.record_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = LogCertification.log_user_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u2',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u2.id = LogCertification.user_id',
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w.id = LogCertification.workstation_id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.id = w.line_id',
                    ),
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'INNER',
                    'conditions' => array(
                        'a.id = l.area_id',
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'INNER',
                    'conditions' => array(
                        'm.id = a.moe_id',
                    ),
                ),
            ),
            'conditions' => array(
                'LogCertification.log_type' => 3,
                'LogCertification.log_user_id' => $userId,
                'LogCertification.creation_date >=' => $startDate->format('Y-m-d'),
                'LogCertification.creation_date <=' => $endDate->format('Y-m-d'),
            ),
            'order' => array(
                'LogCertification.log_creation_date DESC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

}
